<template>
	<div class="app-container calendar-list-container">
		<div class="filter-container">
			<el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="参数名称" v-model.trim="whSystemParameter.paramid"> </el-input>
			<el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
			<el-button class="filter-item" v-if="whSystemParameterManager_btn_add" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
		</div>
		<el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
			<el-table-column type="index" label="序号" header-align="center" align="center" width="65">
				<template scope="scope"><span>{{scope.$index+(whSystemParameter.page - 1) * whSystemParameter.limit + 1}} </span></template>
			</el-table-column>
			<el-table-column align="center" label="id" width="65" v-if="false">
				<template scope="scope">
					<span>{{scope.row.id}}</span>
				</template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="参数名称">
				<template scope="scope">
					<span>{{scope.row.paramid}}</span>
				</template>
			</el-table-column>
			<el-table-column width="500px" align="center" label="参数值">
				<template scope="scope">
					<span>{{scope.row.paramcontent}}</span>
				</template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="类型">
				<template scope="scope">
					<span>{{scope.row.paramtype}}</span>
				</template>
			</el-table-column>
			<el-table-column width="200px" align="center" label="备注">
				<template scope="scope">
					<span>{{scope.row.memo}}</span>
				</template>
			</el-table-column>
			<el-table-column fixed="right" align="center" label="操作" width="150">
				<template scope="scope">
					<el-button v-if="whSystemParameterManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
					</el-button>
					<el-button v-if="whSystemParameterManager_btn_view" size="small" type="success" @click="handleRedis(scope.row)">redis
					</el-button>
					<el-button v-if="whSystemParameterManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div v-show="!listLoading" class="pagination-container">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="whSystemParameter.page" :page-sizes="[10,20,30, 50]" :page-size="whSystemParameter.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
		</div>
		<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
			<el-form :model="form" :rules="rules" ref="form" label-width="110px">

				<el-form-item label="参数名称" prop="paramid">
					<el-input v-model="form.sql.paramid" placeholder="请输入参数名称。必须是唯一"></el-input>
				</el-form-item>

				<el-form-item label="参数值" prop="paramcontent">
					<el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="form.sql.paramcontent" placeholder="请输入参数值"></el-input>
					<!-- <el-tooltip class="item" effect="dark" content="redis緩存数据" placement="left">
      	<el-input  type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-if="dialogStatus=='view'"  v-model="form.redis.paramcontent" placeholder="请输入参数值"></el-input>
     	</el-tooltip>-->
				</el-form-item>

				<el-form-item label="类型" prop="paramtype">
					<el-input v-model="form.sql.paramtype" placeholder="请输入类型"></el-input>
				</el-form-item>

				<el-form-item label="备注" prop="memo">
					<el-input v-model="form.sql.memo" placeholder="请输入备注"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel('form')">取 消</el-button>
				<el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
				<el-button v-if="dialogStatus=='update'" type="primary" @click="update('form')">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisiblelView">
			<el-form :model="form" :rules="rules" ref="form" label-width="110px">

				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" prop="paramid" style="margin-bottom:0px">
					<el-input v-model="form.redis.paramid" readonly></el-input>
				</el-form-item>
				<el-form-item label="参数名称" prop="paramid">
					<el-input v-model="form.sql.paramid" readonly></el-input>
				</el-form-item>

				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" prop="paramcontent" style="margin-bottom:0px">
					<el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="form.redis.paramcontent" readonly></el-input>
				</el-form-item>
				<el-form-item label="参数值" prop="paramcontent">
					<el-input type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-model="form.sql.paramcontent" readonly></el-input>
					<!-- <el-tooltip class="item" effect="dark" content="redis緩存数据" placement="left">
      	<el-input  type="textarea" :autosize="{ minRows: 4, maxRows: 6}" v-if="dialogStatus=='view'"  v-model="form.redis.paramcontent" placeholder="请输入参数值"></el-input>
     	</el-tooltip>-->
				</el-form-item>

				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" prop="paramtype" style="margin-bottom:0px">
					<el-input v-model="form.redis.paramtype" readonly></el-input>
				</el-form-item>
				<el-form-item label="类型" prop="paramtype">
					<el-input v-model="form.sql.paramtype" readonly></el-input>
				</el-form-item>

				<el-form-item v-if="dialogStatus=='view'" label="redis緩存数据" prop="memo" style="margin-bottom:0px">
					<el-input v-model="form.redis.memo" readonly></el-input>
				</el-form-item>
				<el-form-item label="备注" prop="memo">
					<el-input v-model="form.sql.memo" readonly></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button v-if="dialogStatus=='view'" type="primary" @click="cancelView('form')">确 定</el-button>
				<el-button v-if="dialogStatus=='view'" type="danger" @click="deleteRedis('form')">删除redis</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		page,
		addObj,
		getObj,
		getObjRedis,
		deleteRedis,
		delObj,
		putObj
	} from 'api/admin/whSystemParameter/index';
	import { mapGetters } from 'vuex';
	export default {
		name: 'whSystemParameter',
		data() {
			return {
				form: {
					sql: {
						paramid: undefined,
						paramcontent: undefined,
						paramtype: undefined,
						memo: undefined
					},
					redis: {
						paramid: undefined,
						paramcontent: undefined,
						paramtype: undefined,
						memo: undefined
					}
				},
				rules: {
					/*  paramid: [
  {
    required: true,
    message: '请输入参数名称。必须是唯一的',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   paramcontent: [
  {
    required: true,
    message: '请输入参数值',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   paramtype: [
  {
    required: true,
    message: '请输入类型',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   memo: [
  {
    required: true,
    message: '请输入备注',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
]    */
				},
				list: null,
				total: null,
				listLoading: true,
				tableName: '',
				whSystemParameter: {
					page: 1,
					limit: 10
				},
				dialogFormVisible: false,
				dialogFormVisiblelView: false,
				dialogStatus: '',
				whSystemParameterManager_btn_edit: false,
				whSystemParameterManager_btn_del: false,
				whSystemParameterManager_btn_add: false,
				whSystemParameterManager_btn_view: false,
				textMap: {
					update: '编辑',
					create: '创建'
				},
				tableKey: 0
			}
		},
		created() {
			this.getList();
			this.whSystemParameterManager_btn_edit = this.elements['whSystemParameterManager:btn_edit'];
			this.whSystemParameterManager_btn_del = this.elements['whSystemParameterManager:btn_del'];
			this.whSystemParameterManager_btn_add = this.elements['whSystemParameterManager:btn_add'];
			this.whSystemParameterManager_btn_view = this.elements['whSystemParameterManager:view'];
		},
		computed: {
			...mapGetters([
				'elements'
			])
		},
		methods: {
			getList() {
				this.listLoading = true;
				page(this.whSystemParameter)
					.then(response => {
						this.list = response.data.rows;
						this.total = response.data.total;
						this.listLoading = false;
					})
			},
			handleFilter() {
				this.getList();
			},
			handleSizeChange(val) {
				this.whSystemParameter.limit = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.whSystemParameter.page = val;
				this.getList();
			},
			handleCreate() {
				this.resetTemp();
				this.dialogStatus = 'create';
				this.dialogFormVisible = true;
			},
			handleUpdate(row) {
				this.form.sql = row;
				getObj(this.form.sql.id)
					.then(response => {
						this.form.sql = response.data;
						this.dialogStatus = 'update';
						this.dialogFormVisible = true;
					});
			},
			handleRedis(row) {
				this.form.sql = row;
				this.tableName = 'wh_system_parameter';
				getObjRedis(this.form.sql, this.tableName)
					.then(response => {
						if(response.status == 0) {
							this.form.redis = response.data;
							//        	this.form.redis = this.form.sql;
							this.dialogStatus = 'view';
							this.dialogFormVisiblelView = true;
						} else {
							this.$notify({
								title: '查询失败：',
								message: response.message,
								type: 'warning',
								duration: 3000

							});
						}
					});

			},
			handleDelete(row) {
				this.$confirm('此操作将永久删除, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						delObj(row.id)
							.then(() => {
								this.$notify({
									title: '成功',
									message: '删除成功',
									type: 'success',
									duration: 2000
								});
								const index = this.list.indexOf(row);
								this.list.splice(index, 1);
							});
					});
			},
			create(formName) {
				const set = this.$refs;
				set[formName].validate(valid => {
					if(valid) {
						addObj(this.form.sql)
							.then((result) => {
								if(result.status == 0) {
									this.dialogFormVisible = false;
									this.getList();
									this.$notify({
										title: '成功',
										message: result.message,
										type: 'success',
										duration: 2000
									});
								} else {
									this.$notify({
										title: '失败',
										message: result.message,
										type: 'warning',
										duration: 2000
									});
								}
							})
					} else {
						return false;
					}
				});
			},
			cancel(formName) {
				this.dialogFormVisible = false;
				const set = this.$refs;
				set[formName].resetFields();
			},
			cancelView(formName) {
				this.dialogFormVisiblelView = false;
				const set = this.$refs;
				set[formName].resetFields();
			},
			update(formName) {
				const set = this.$refs;
				set[formName].validate(valid => {
					if(valid) {
						this.form.password = undefined;
						putObj(this.form.sql.id, this.form.sql).then((result) => {
							if(result.status == 0) {
								this.dialogFormVisible = false;
								this.getList();
								this.$notify({
									title: '成功',
									message: result.message,
									type: 'success',
									duration: 2000
								});
							} else {
								this.$notify({
									title: '失败',
									message: result.message,
									type: 'warning',
									duration: 2000
								});
							}
						});
					} else {
						return false;
					}
				});
			},
			deleteRedis(formName) {
				this.$confirm('此操作将删除redis数据?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					const set = this.$refs;
					set[formName].validate(valid => {
						if(valid) {
//							this.dialogFormVisible = false;
							this.form.password = undefined;
							this.tableName = 'wh_system_parameter';
							deleteRedis(this.form.sql, this.tableName).then((result) => {

								//      console.log(result.data);
								this.dialogFormVisiblelView = false;
								this.getList();
								if(result.status == 0) {
									this.$notify({
										title: '操作成功',
										message: result.message,
										type: 'success',
										duration: 3000
									});
								} else {
									this.$notify({
										title: '操作失败:',
										message: result.message,
										type: 'warning',
										duration: 3000
									});
								}
								/*this.$notify({
								title: '成功',
								message: '更新成功',
								type: 'success',
								duration: 2000
								});*/
							});
						} else {
							return false;
						}
					});
				});
			},
			resetTemp() {
				this.form.sql = {
					username: undefined,
					name: undefined,
					sex: '男',
					password: undefined,
					description: undefined
				};
			}
		}
	}
</script>